package cn.yh.register.user.api;

import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.AuthContextHolder;
import cn.yh.register.common.util.IpUtil;
import cn.yh.register.model.user.UserInfo;
import cn.yh.register.user.service.UserInfoService;
import cn.yh.register.vo.user.LoginVo;
import cn.yh.register.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/api/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public Result<Map<String, Object>> loign(@RequestBody LoginVo loginVo, HttpServletRequest request){
        //获取ip 把ip设置进去
        String ipAddr = IpUtil.getIpAddr(request);
        loginVo.setIp(ipAddr);
        Map<String, Object> login = userInfoService.login(loginVo);
        return Result.ok(login);
    }


    @ApiOperation("用户认证接口")
    @PostMapping("/auth/userAuth")
    public Result<?> userAuth(@RequestBody UserAuthVo userAuthVo,HttpServletRequest request){
        //拿到用户id
        Long userId = AuthContextHolder.getUserId(request);
        userInfoService.userAuth(userId,userAuthVo);
        return Result.ok();
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/auth/getUserInfo")
    public Result<UserInfo> getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfo);
    }
}
